package kernel;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Region of Interest: <br>
 * Is a cell with information:<br>
 * <ul>
 * <li> A set of fiber ids that pass by this cell
 * <li> A set of ROIs that this ROI has connection with (They at least 1 fiber in common)
 * </ul>
 */
public class ROI {
	private int[] roiPosition;
	private Set<Integer> fiberIds;
	private Set<ROI> connectivitySet;
	private Map<Fiber, Integer> fibers;
	
	
	/**
	 * Basic constructor
	 * @param i position in i
	 * @param j position in j
	 * @param k position in k
	 */
	public ROI(int i, int j, int k)
	{
		roiPosition = new int[3];
		roiPosition[0] = i;
		roiPosition[1] = j;
		roiPosition[2] = k;
		fiberIds = new HashSet<Integer>();
		fibers = new HashMap<Fiber, Integer>();
		connectivitySet = new HashSet<ROI>();
	}
	/**
	 * Adds a new ROI in the set of connectivity
	 * @param newRoi new ROI that connects with this one
	 */
	public void addROI(ROI newRoi)
	{
		Iterator<ROI> it = connectivitySet.iterator();
		boolean found = false;
		while(it.hasNext() && !found)
		{
			ROI r = it.next();
			if(r.equals(newRoi))
			{
				found = true;
			}
		}
		if(!found)
		{
			connectivitySet.add(newRoi);
		}
	}
	
	/**
	 * Adds a new fiber that pass by this ROI
	 * @param fiberId the fiber id
	 */
	public void addFiberId(int fiberId)
	{
		fiberIds.add(fiberId);
	}
	
	public void addFiber(Fiber fiber)
	{
		if(fibers.containsKey(fiber))
		{
			int pointsCount = fibers.get(fiber);
			pointsCount++;
			fibers.put(fiber, pointsCount);
		}
		else
		{
			fibers.put(fiber, 1);
		}
	}

	/**
	 * Return the set of fiber ids that pass by this ROI
	 * @return the set of fiber ids that pass by this ROI
	 */
	public Set<Integer> getFiberIds() {
		return fiberIds;
	}
	
	/**
	 * Returns the set of ROIs that have connection with this one
	 * @return the set of ROIs that have connection with this one
	 */
	public Set<ROI> getConnectivitySet()
	{
		return connectivitySet;
	}
	
	/**
	 * Returns if the fiber pass by this ROI
	 * @param fiberId fiber identifier
	 * @return if the fiber pass by this ROI
	 */
	public boolean containFiber(int fiberId)
	{
		return fiberIds.contains(fiberId);
	}
	/**
	 * @return the roiPosition
	 */
	public int[] getRoiPosition() {
		return roiPosition;
	}
	
	/**
	 * Returns if another ROI is the same one that this (has the same position)
	 * @param otherRoi
	 * @return
	 */
	public boolean equals(ROI otherRoi)
	{
		int[] otherRoiPos = otherRoi.getRoiPosition();
		if(otherRoiPos[0] == roiPosition[0] && otherRoiPos[1] == roiPosition[1] && otherRoiPos[2] == roiPosition[2])
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/**
	 * @return the fibers
	 */
	public Map<Fiber, Integer> getFibers() {
		return fibers;
	}
	

}
